package com.ruoyi.web.controller.credit;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysRegisterService;
import com.ruoyi.system.service.ICreditUserInfoService;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;

/**
 * 信用系统注册验证
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/credit")
public class CreditRegisterController extends BaseController
{
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private ICreditUserInfoService creditUserInfoService;

    @Autowired
    private ISysUserService userService;

    @PostMapping("/register")
    public AjaxResult register(@RequestBody CreditRegisterBody registerBody)
    {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser"))))
        {
            return error("当前系统没有开启注册功能！");
        }
        
        // 验证手机号
        if (!StringUtils.hasText(registerBody.getPhonenumber()))
        {
            return error("手机号不能为空");
        }
        
        // 验证身份证号
        if (!StringUtils.hasText(registerBody.getIdCard()))
        {
            return error("身份证号不能为空");
        }
        
        // 检查手机号是否已被注册
        if (creditUserInfoService.selectCreditUserInfoByPhonenumber(registerBody.getPhonenumber()) != null)
        {
            return error("该手机号已被注册");
        }
        
        // 检查身份证号是否已被注册
        if (creditUserInfoService.selectCreditUserInfoByIdCard(registerBody.getIdCard()) != null)
        {
            return error("该身份证号已被注册");
        }

        // 执行注册
        String msg = registerService.register(registerBody);
        if (!com.ruoyi.common.utils.StringUtils.isEmpty(msg))
        {
            return error(msg);
        }
        
        // 注册成功后，初始化用户信用信息
        try {
            // 根据用户名查询用户
            SysUser queryUser = new SysUser();
            queryUser.setUserName(registerBody.getUsername());
            List<SysUser> users = userService.selectUserList(queryUser);
            if (!users.isEmpty()) {
                SysUser user = users.get(0);
                // 更新用户手机号
                user.setPhonenumber(registerBody.getPhonenumber());
                userService.updateUser(user);
                // 初始化用户信用信息
                creditUserInfoService.initUserCreditInfo(user.getUserId(), registerBody.getIdCard());
            }
        } catch (Exception e) {
            logger.error("初始化用户信用信息失败", e);
        }
        
        return success("注册成功");
    }

    /**
     * 信用系统注册请求体
     */
    public static class CreditRegisterBody extends RegisterBody
    {
        /** 手机号 */
        private String phonenumber;
        
        /** 身份证号 */
        private String idCard;

        public String getPhonenumber() {
            return phonenumber;
        }

        public void setPhonenumber(String phonenumber) {
            this.phonenumber = phonenumber;
        }

        public String getIdCard() {
            return idCard;
        }

        public void setIdCard(String idCard) {
            this.idCard = idCard;
        }
    }
} 